package cc.rengu.igas.momp.common.util;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.oltp.utility.util.DateUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期-周计算工具方法
 *
 * @author Jinan Liu
 * @since 2020/4/14 14:01
 */

public class MompDateUtil {

    /**
     * 计算当前日期是一周里的第几天
     * <p>
     * Monday - 1
     * Tuesday - 2
     * Wednesday - 3
     * Thursday - 4
     * Friday - 5
     * Saturday - 6
     * Sunday - 7
     *
     * @param sdate 待判断日期
     * @return int 返回码
     * @author Jinan Liu
     * @date 2020/7/7 18:18
     */
    public int getWeek(String sdate) {

        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int week = c.get(Calendar.DAY_OF_WEEK);
        /* hour中存的就是星期几了,其范围 1~7,1=星期日 7=星期六，其他类推 */
        if (1 == week) {
            return 7;
        } else {
            return week - 1;
        }
    }

    /**
     * 时间日期转换
     *
     * @param strDate 待判断日期
     * @return java.util.Date 日期
     * @author Jinan Liu
     * @date 2020/7/7 18:18
     */
    public Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 判断当前日期是否在指定日期范围内
     * 在日期范围内返回0
     * 未在日期范围内返回-1
     *
     * @param currentDate 当前日期
     * @param startDate   起始日期
     * @param endDate     结束日期
     * @return int 返回码
     * @author Jinan Liu
     * @since 2020/4/17 18:06
     */
    public static int checkDateRange(String currentDate, String startDate, String endDate) {

        BigDecimal currentDateNum = new BigDecimal(currentDate).setScale(CommonConstant.ZERO, RoundingMode.HALF_UP);
        BigDecimal startDateNum = new BigDecimal(startDate).setScale(CommonConstant.ZERO, RoundingMode.HALF_UP);
        BigDecimal endTimeNum = new BigDecimal(endDate).setScale(CommonConstant.ZERO, RoundingMode.HALF_UP);

        if ((currentDateNum.compareTo(startDateNum) != CommonConstant.IS_LESS_THAN) &&
                (currentDateNum.compareTo(endTimeNum) != CommonConstant.IS_GREATER_THAN)) {
            /* 当前交易时间在营销活动时间范围内,时间规则判断通过 */
            return CommonConstant.PROCESS_SUCCESS;
        } else {
            /* 当前交易时间未在营销活动时间范围内,时间规则判断不通过 */
            return CommonConstant.PROCESS_FAILED;
        }
    }

    /**
     * 判断当前日期是否在指定日期范围内
     * 在日期范围内返回true
     * 未在日期范围内返回false
     *
     * @param currentDate 当前日期
     * @param endDate     结束日期
     * @return boolean 返回码
     * @author Jinan Liu
     * @date 2020/7/28 15:49
     */
    public static boolean checkDateRange(String currentDate, String endDate) {

        BigDecimal currentDateNum = new BigDecimal(currentDate).setScale(CommonConstant.ZERO, RoundingMode.HALF_UP);
        BigDecimal endTimeNum = new BigDecimal(endDate).setScale(CommonConstant.ZERO, RoundingMode.HALF_UP);

        if (CommonConstant.ZERO >= currentDateNum.compareTo(endTimeNum)) {
            /* 当前交易时间在营销活动时间范围内,时间规则判断通过 */
            return true;
        } else {
            /* 当前交易时间未在营销活动时间范围内,时间规则判断不通过 */
            return false;
        }
    }

    /**
     * 获取当月第一天日期
     *
     * @return java.lang.String 当月第一天日期
     * @author Jinan Liu
     * @date 2020/7/7 18:22
     */
    public static String getCurrentMonthFirstDay() {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    /**
     * 获取当月最后一天日期
     *
     * @return java.lang.String 当月最后一天日期
     * @author Jinan Liu
     * @date 2020/7/7 18:22
     */
    public static String getCurrentMonthLastDay() {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return format.format(calendar.getTime());
    }

    /**
     * 获取任意月的第2天
     *
     * @return java.lang.String 获取当月的第二天
     * @author liujinan
     * @date 2020/9/4 18:22
     */
    public static String getSecondDay(int year, int month) {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 2);
        return format.format(calendar.getTime());
    }

    /**
     * 获取任意月的第1天
     *
     * @return java.lang.String 获取任意月的第1天
     * @author liujinan
     * @date 2021/3/25 18:22
     */
    public static String getFirstDay(int year, int month) {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    /**
     * 获取任意月的下一个月的第1天
     *
     * @return java.lang.String 获取任意月的下一个月的第1天
     * @author liujinan
     * @date 2020/9/4 18:22
     */
    public static String getNextMonthFirstDay(int year, int month) {

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    /**
     * 获取某日期指定月份后的日期
     *
     * @param sourceDate 指定日期
     * @param sourceDate 指定月份
     * @return 指定月份后的日期
     */
    public static String getFutureMonDay(String sourceDate, int nextMonth) {
        Date date = getDateStamp(sourceDate, "yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, nextMonth);
        return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
    }

    /**
     * 获取任何一个月份
     *
     * @param nextMonth 增减月份值
     * @return 指定月份后的日期
     */
    public static String getEveryMonth(int nextMonth) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, nextMonth);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    /**
     * 获取某日期指定月份后的日期，15号之前直接到上月月底，15号之后到本月月底
     *
     * @param sourceDate 指定日期yyyyMMdd
     * @param sourceDate 指定月份
     * @return 指定月份后的日期
     */
    public static String getFutureMonLastDay(String sourceDate, int nextMonth) {
        //获取日期的日 20200529
        String day = sourceDate.substring(6, 8);

        Date date = getDateStamp(sourceDate, "yyyyMMdd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, nextMonth);

        if (Integer.parseInt(day) > 15) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
        } else {
            calendar.add(Calendar.MONTH, -1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
        }
    }

    /**
     * 获取某日期指定月份后的日期(月份+1)，15号之前直接到本月第一天，15号之后到下月第一天
     *
     * @param sourceDate 指定日期yyyyMMdd
     * @param sourceDate 指定月份
     * @return 指定月份后的日期
     */
    public static String getFutureMonAddOneFirstDay(String sourceDate, int nextMonth) {
        //获取日期的日 20200529
        String day = sourceDate.substring(6, 8);

        Date date = getDateStamp(sourceDate, "yyyyMMdd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, nextMonth + 1);

        if (Integer.parseInt(day) > 15) {
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
        } else {
            calendar.add(Calendar.MONTH, -1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
        }
    }

    /**
     * 将日期字符串转换成日期
     *
     * @param dateString 日期字符串
     * @param format     日期格式
     * @return 日期
     */
    public static Date getDateStamp(String dateString, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * 获取指定年的月的天数
     *
     * @param year  年
     * @param month 月
     * @return 天数
     */
    public static int getMonthLastDay(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DATE, 1);
        calendar.roll(Calendar.DATE, -1);
        int maxDate = calendar.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取当前时间所属季度结束月最后一天
     *
     * @param endDate 当前时间
     * @return
     */
    public static String quarterEnd(String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dEnd = null;
        try {
            dEnd = sdf.parse(endDate);
        } catch (Exception e) {

        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dEnd);
        int remainder = (calendar.get(Calendar.MONTH) + 1) % 3;
        int month = remainder != 0 ? calendar.get(Calendar.MONTH) + (3 - remainder) : calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.setTime(calendar.getTime());
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取当年最后一天
     *
     * @return
     */
    public static String getYearLastDay() {
        Calendar calendar = Calendar.getInstance();
        Calendar currCal = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(time);
    }

    /**
     * 获取当前月
     *
     * @return
     */
    public static String getMonth() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        try {
            date = dateFormat.parse(DateUtil.getCurrentDate().substring(0, 6));
        } catch (ParseException e) {

        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        date = calendar.getTime();
        String accDate = simpleDateFormat.format(date);
        return accDate;
    }

    /**
     * 获取当前月的前N个月份
     *
     * @return
     */
    public static String getLast12Month(int i) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        try {
            date = dateFormat.parse(DateUtil.getCurrentDate().substring(0, 6));
        } catch (ParseException e) {

        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -i);
        date = calendar.getTime();
        String accDate = simpleDateFormat.format(date);
        return accDate;
    }

    /**
     * 1970到当前日期的秒数
     *
     * @return
     */
    public static String getTime() {
        Calendar.getInstance().getTimeInMillis();
        return String.valueOf(Calendar.getInstance().getTimeInMillis());
    }

    /**
     * 获取任意月的最后一天
     *
     * @return java.lang.String 获取任意月的最后一天
     * @author JL PANG
     * @date 2020/10/27 13:22
     */
    public static String getMonthTheLastDay(String month) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        try {
            Date nowDate = format.parse(month);
            calendar = Calendar.getInstance();
            calendar.setTime(nowDate);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(calendar.getTime());
    }

    /**
     * 根据基础日期获取偏移月份对应的日期
     *
     * @param baseDate 基础日期
     * @param offset   偏移量
     * @return 偏移月份后对应的日期
     */
    public static String getBaseDateOffsetMonth(String baseDate, int offset) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
            Calendar calendar = Calendar.getInstance();
            Date date = simpleDateFormat.parse(baseDate);
            calendar.setTime(date);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + offset);
            return simpleDateFormat.format(calendar.getTime());
        } catch (ParseException e) {

        }
        return null;
    }

    /**
     * 获取某日期指定月份后的日期月的第一天
     *
     * @param sourceDate 指定日期yyyyMMdd
     * @param sourceDate 指定月份
     * @return 指定月份后的日期
     */
    public static String getFutureMonFirstDay(String sourceDate, int nextMonth) {
        Date date = getDateStamp(sourceDate, "yyyyMMdd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.MONTH, nextMonth);

        return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
    }
}
